<template>
	<el-container>
		<el-container>
			<el-header>
				<div class="left-panel">
					<el-button type="primary" icon="el-icon-plus" @click="add" v-auth="'staff.add'">新增</el-button>
                    <el-button type="primary" @click="unBind" :disabled="selection.length == 0" v-auth="'staff.unBind'">批量解绑账号</el-button>
				</div>
                <div class="right-panel">
                    <div class="right-panel-search">
                        <el-input v-model="search.name" placeholder="员工姓名" clearable @change="handleKeyDown"></el-input>
                        <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                    </div>
                </div>
			</el-header>
			<el-main class="nopadding">
				<scTable ref="table" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort remoteFilter border>
					<el-table-column type="selection" align="center"></el-table-column>
					<el-table-column label="员工姓名" prop="name" align="center"></el-table-column>
					<el-table-column label="登录账户" prop="account" align="center"></el-table-column>
					<el-table-column label="部门" prop="dept_id" align="center" sortable>
                        <template #default="scope">
                            <div>{{scope.row.dept_name}}</div>
						</template>
                    </el-table-column>
					<el-table-column label="银行" prop="bank_name" align="center"></el-table-column>
					<el-table-column label="银行卡号" prop="bank_card" align="center"></el-table-column>
					<el-table-column label="岗位" prop="jobs_name" align="center"></el-table-column>
					<el-table-column label="入职日期" prop="entry_date" align="center"></el-table-column>
					<el-table-column label="薪资结算方式" prop="settlement_method" align="center">
                        <template #default="scope">
                            <div v-if="scope.row.settlement_method==1">计时</div>
                            <div v-else-if="scope.row.settlement_method==2">计件</div>
                            <div v-else>计时+计件</div>
						</template>
                    </el-table-column>
					<el-table-column label="是否禁用" prop="disable" align="center">
                        <template #default="scope">
							<el-tag v-if="scope.row.disable==1" type="danger">是</el-tag>
						    <el-tag v-else type="success">否</el-tag>
						</template>
                    </el-table-column>
					<el-table-column label="操作" align="center" width="100">
						<template #default="scope">
							<el-button-group style="display:flex;flex-direction:column">
                                <el-popconfirm title="确定离职该员工吗？" @confirm="table_resignation(scope.row, scope.$index)" v-if="scope.row.dept_id.indexOf(40) === -1">
									<template #reference>
								        <el-button text type="primary" size="small" v-auth="'staff.resignation'">离职</el-button>
									</template>
								</el-popconfirm>
								<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)" v-auth="'staff.edit'" v-if="scope.row.dept_id.indexOf(40) === -1">修改</el-button>
								<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
									<template #reference>
										<el-button text type="danger" size="small" v-auth="'staff.delete'">删除</el-button>
									</template>
								</el-popconfirm>
                                <!-- <el-popconfirm title="确定解绑该账号吗？" @confirm="unBind(scope.row, scope.$index)">
									<template #reference>
										<el-button text type="warning" size="small">解绑账号</el-button>
									</template>
								</el-popconfirm> -->
							</el-button-group>
						</template>
					</el-table-column>
				</scTable>
			</el-main>
		</el-container>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false" draggable></save-dialog>
</template>

<script>
import saveDialog from './save'
export default {
	name: 'user',
	components: {
		saveDialog,
	},
	data() {
		return {
			dialog: {
				save: false
			},
			groupFilterText: '',
			group: [],
			apiObj: this.$API.setting.staff.lists,
			selection: [],
			search: {
				title: null,
				cid: null,
			},
			//所需数据选项
			cates: [],
            text:"",
		}
	},
	watch: {
		groupFilterText(val) {
			this.$refs.group.filter(val);
		}
	},
	mounted() {
	},
	methods: {
		//添加
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add')
			})
		},
		//编辑
		table_edit(row) {
            console.log('row :>> ', row);
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
        async table_resignation(row) {
			var reqData = { id: row.id }
			var res = await this.$API.setting.staff.depart.post(reqData);
			if (res.code == 1) {
				//这里选择刷新整个表格 OR 插入/编辑现有表格数据
                this.$refs.table.refresh()
				this.$message.success("操作成功")
			} else {
				this.$alert(res.message, "提示", { type: 'error' })
			}
		},
        goLocation(){
            this.$router.push({
                path: 'storeroomLocation',
            })
        },
        async unBindAll(delete_ids) {
			var reqData = { ids: delete_ids }
			var res = await this.$API.admin.unBindAll.post(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
				this.$message.success("解绑成功")
			} else {
				this.$alert(res.message, "提示", { type: 'error' })
			}
		},
		//批量解绑
		async unBind() {
			var ids = [];
			this.selection.forEach((item) => {
				ids.push(item.id)
			})
			this.$confirm(`确定解绑选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(() => {
				this.unBindAll(ids)
			}).catch(() => {

			})
		},
		//删除
		async table_del(row, index) {
			var reqData = { id: row.id }
			var res = await this.$API.setting.staff.delete.post(reqData);
			if (res.code == 1) {
				//这里选择刷新整个表格 OR 插入/编辑现有表格数据
				this.$refs.table.tableData.splice(index, 1);
                this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", { type: 'error' })
			}
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
        handleKeyDown(){
            this.upsearch()
        },
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			} else if (mode == 'set') {
				this.$refs.table.refresh()
			}
		},
        selectionChange(selection) {
			this.selection = selection;
		},
	}
}
</script>

<style>
.search-input {
	width: auto;
	font-size: 14px;
}
</style>
